import { BoxGeometry, Mesh, MeshBasicMaterial, Sprite } from 'three';
import { BaseModel, Params } from './baseModel.js';
import { WORDS } from './WORDS.js';
import { Helper } from '../helper.js';

interface DeviceParams extends Params {
    depth: number,
    width: number,
    height: number,
    color: string,
    position: [number, number, number]
}

class Device {
    id: string
    obj: Mesh<BoxGeometry, MeshBasicMaterial>
    type: string
    platformIndex?: number
    params: DeviceParams
    constructor(obj: Mesh<BoxGeometry, MeshBasicMaterial>, params: DeviceParams) {
        if (!obj.isMesh) {
            throw new Error(WORDS.ERROR.NOT_MESH);
        }

        if (params.id) {
            this.id = params.id
        } else {
            this.id = Date.now().toString()
            params.id = this.id
        }

        this.params = params
        this.obj = obj
        this.obj.name = this.id
        this.type = WORDS.OBJNAME.MODEL_DEVICE
        this.obj.userData.type = this.type
    }

    setOpacity(val: number) {
        (this.obj.material as MeshBasicMaterial).opacity = val
        this.params.opacity = val
    }

    updateParams() {
        this.params.position = this.obj.position.toArray()
        this.params.color = Helper.getHexColorStr(this.obj.material.color)
        this.params.width = this.obj.geometry.parameters.width
        this.params.height = this.obj.geometry.parameters.height
        this.params.depth = this.obj.geometry.parameters.depth
        this.params.opacity = this.obj.material.opacity
    }

    toJSON() {
        this.updateParams()
        return this.params
    }
}

export { Device };
export type { DeviceParams };
